package edu.ncet.desktop.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.charts.client.model.Scale;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;

import edu.ncet.desktop.shared.BarChartResultModel;
import edu.ncet.desktop.shared.CurrentExamResult;
import edu.ncet.desktop.shared.ResultModel;

public class MarksCardDisplayScreen extends ContentPanel {
	
	private ResultModel resultModel;
	
	protected Map<String,Integer> resultMap = new HashMap<String, Integer>();
	protected List<BarChartResultModel> historyComparision = new ArrayList<BarChartResultModel>();
	
	protected HashMap<Integer, String> typeNoTypeMap = new HashMap<Integer, String>();
	protected ListStore<BarChartResultModel> barchartHistoryCompModels = new ListStore<BarChartResultModel>();
	protected ContentPanel panel;

	public MarksCardDisplayScreen(ResultModel resModel)
	{
		this.resultModel  = resModel;
		createResultMap();
		createTypeNoTypeMap();
		createHisoryComparision();
		createSelector();
	}
	
	protected void createSelector() {
		setHeaderVisible(false);
		GlobalItem.getExamWindow().setSize(820, 608);
		
		Grid<ResultGridModel> resultGrid = getResultGrid();
		ContentPanel historyResultChartPanel = addHistoryComparisionChart();
		ContentPanel currentResultChartPanel = addCurrentResultChart( );
		setLayout(new BorderLayout());
		
		BorderLayoutData layoutData = new BorderLayoutData(LayoutRegion.NORTH);
		layoutData.setCollapsible(true);
		add(resultGrid, layoutData);
		
		layoutData = new BorderLayoutData(LayoutRegion.EAST);
		layoutData.setSize(400);
		layoutData.setCollapsible(true);
		add(historyResultChartPanel, layoutData);

		layoutData = new BorderLayoutData(LayoutRegion.WEST);
		layoutData.setSize(400);
		layoutData.setCollapsible(true);
		add(currentResultChartPanel, layoutData);
		
		layout(true);
	}

	private Grid<ResultGridModel> getResultGrid() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(new ColumnConfig("type", "Type", 200));
		columns.add(new ColumnConfig("totalQues", "Total Questions", 200));
		columns.add(new ColumnConfig("correctQues", "Correct Questions", 200));
		columns.add(new ColumnConfig("percentage", "Percentage", 200));
		
		ColumnModel model = new ColumnModel(columns);
		ListStore<ResultGridModel> store = new ListStore<ResultGridModel>();
		
		for (String type : resultMap.keySet()) {
			ResultGridModel model1 = new ResultGridModel();
			model1.setType(type);
			model1.setTotalQues(resultModel.getNumberOfQuestions(type));
			model1.setCorrectQues(maxMarks = resultMap.get(type));
			model1.setPercentage(resultModel.getPercentageForCurrentTypeNo(resultModel.getTypeNo(type)));
			store.add(model1);
		}
		
		Grid<ResultGridModel> resultGrid = new Grid<ResultGridModel>(store, model);
		resultGrid.setHeight(resultMap.keySet().size() * 25 + 30 );
		return resultGrid;
	}

	private void createResultMap() {
		for(CurrentExamResult resModel : resultModel.getCurrResults())
		{
			String type = resModel.getTypeName();
			if(!resultMap.containsKey(type))
			{
				resultMap.put(type,0);
			}
			if(resModel.getCorrectAns().equals(resModel.getWrittenAns()))
			{
				resultMap.put(type, resultMap.get(type) + 1);
			}
		}
	}
	
	//Creates the map TypeNo --> TypeName
	private void createTypeNoTypeMap() {
		typeNoTypeMap = new HashMap<Integer,String>();
		for(CurrentExamResult res : resultModel.getCurrResults())
		{
			typeNoTypeMap.put(res.getTypeNo(),res.getTypeName());
		}
	}

	// Calculate the difference b/n current result and history percentage and put it as the form of bar
	private void createHisoryComparision() {
		for(Integer typeNo : resultModel.getCurrentTypeNos())
		{
			final Integer historyPercentile = resultModel.getHistoryPercentageForType(typeNo);
			if(historyPercentile != -1)
			{
				barchartHistoryCompModels.add(new BarChartResultModel(resultModel.getPercentageForCurrentTypeNo(typeNo) - historyPercentile , typeNoTypeMap.get(typeNo)));
			}
		}
	}

	protected ContentPanel addHistoryComparisionChart() {
		ContentPanel graphPanel = new ContentPanel();
		GraphHelper.drawGraph(graphPanel, "History Comparision", barchartHistoryCompModels, new Scale(-100,100,10));
		graphPanel.setFrame(true);
		graphPanel.setHeaderVisible(false);
		return graphPanel;
	}

	protected int maxMarks = 0;
	protected ContentPanel addCurrentResultChart() {
		ContentPanel graphPanel = new ContentPanel();
		ListStore<BarChartResultModel> results =new ListStore<BarChartResultModel>();
		for(String type : resultMap.keySet())
		{
			Integer value = resultMap.get(type);
			maxMarks = value > maxMarks ? maxMarks : value;
			BarChartResultModel result = new BarChartResultModel(value,type);
			results.add(result);
		}
		int value = maxMarks/5 == 0 ? 5 : maxMarks/5;
		GraphHelper.drawGraph(graphPanel, "Current Result", results, new Scale(0,maxMarks + value, value  ) );
		graphPanel.setFrame(true);
		graphPanel.setHeaderVisible(false);
		return graphPanel;
	}
}

